using System.Collections.Generic;
using UnityEngine;

public class PositionFollower : MonoBehaviour
{
	public List<TrailRenderer> trailRenderers = new List<TrailRenderer>();

	public Transform MyTransform;

	public PhysicsBall pBall;

	private Renderer myRenderer;

	private TrailRenderer myTrailRenderer;

	public bool disableRenderWhenBallNotInGame;

	private GameObject myGameObject;

	public GameObject fire;

	private float trailTime;

	protected int activeIndex;

	protected bool useMultipleTrails
	{
		get
		{
			return trailRenderers.Count > 1;
		}
	}

	public void SetColor(Color col, int activeIndex = 0)
	{
		this.activeIndex = activeIndex;
		if (useMultipleTrails)
		{
			foreach (TrailRenderer trailRenderer in trailRenderers)
			{
				Material material = trailRenderer.material;
				col.a = material.GetColor("_Color").a;
				material.SetColor("_Color", col);
			}
			return;
		}
		col.a = myRenderer.material.GetColor("_Color").a;
		myRenderer.material.SetColor("_Color", col);
	}

	public void Init(PhysicsBall ball)
	{
		pBall = ball;
	}

	private void Start()
	{
		MyTransform = base.transform;
		myRenderer = GetComponent<Renderer>();
		myGameObject = base.gameObject;
		myTrailRenderer = GetComponent<TrailRenderer>();
		if (myTrailRenderer != null)
		{
			trailTime = myTrailRenderer.time;
		}
	}

	private void LateUpdate()
	{
		if (pBall != null)
		{
			MyTransform.position = pBall.position;
		}
	}

	private void Update()
	{
		if (pBall == null)
		{
			return;
		}
		MyTransform.position = pBall.position;
		if (disableRenderWhenBallNotInGame && myTrailRenderer != null)
		{
			if (IsRendererEnabled(activeIndex) && !pBall.isBallInGame)
			{
				SetRendererEnabled(false, activeIndex);
				SetTrailTime(0f);
			}
			else if (!IsRendererEnabled(activeIndex) && pBall.isBallInGame)
			{
				SetRendererEnabled(true, activeIndex);
				float num = trailTime / Time.timeScale / GGGameTime.gameTimescale;
				SetTrailTime(num);
			}
		}
	}

	private void SetTrailTime(float time)
	{
		if (useMultipleTrails)
		{
			foreach (TrailRenderer trailRenderer in trailRenderers)
			{
				trailRenderer.time = time;
			}
			return;
		}
		myTrailRenderer.time = time;
	}

	private void SetRendererEnabled(bool enabled, int trailIndex)
	{
		if (useMultipleTrails)
		{
			int num = 0;
			{
				foreach (TrailRenderer trailRenderer in trailRenderers)
				{
					trailRenderer.enabled = enabled && num == trailIndex;
					num++;
				}
				return;
			}
		}
		myTrailRenderer.enabled = enabled;
	}

	private bool IsRendererEnabled(int activeIndex)
	{
		if (useMultipleTrails)
		{
			int num = 0;
			foreach (TrailRenderer trailRenderer in trailRenderers)
			{
				if (num == activeIndex)
				{
					return trailRenderer.enabled;
				}
				num++;
			}
			return false;
		}
		return myTrailRenderer.enabled;
	}
}
